package net

import (
	"GoServer/utils/databases"
	"GoServer/utils/debug"
	"context"
	"fmt"
)

func subscribeToChannel(channel string) {
	pubsub := databases.Rediss.Subscribe(context.Background(), channel)
	_, err := pubsub.Receive(context.Background())
	if err != nil {
		debug.Logln(debug.Error, "Failed to subscribe to channel:", err)
		return
	}

	// 消息接收
	ch := pubsub.Channel()

	for msg := range ch {
		fmt.Println("Received message from channel:", msg.Payload)
		// 根据需求处理消息

	}
}

// 广播消息到 Redis
func BroadcastMessage(channel string, message string) {
	err := databases.Rediss.Publish(context.Background(), channel, message).Err()
	if err != nil {
		fmt.Println("Failed to broadcast message:", err)
	}
}
